package com.dg.acb.utils.xml;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.text.TextUtils;
/**
 *  XML解析读取
 * @author hjwang2
 * @version 1.0
 * @since 2015-8-20
 */
public class XmlResourceParserCtrl implements IXmlCtrl{
    private final String mHomeTag;
    private final XmlResourceParser mXmlParser;

    public XmlResourceParserCtrl(XmlResourceParser xmlResourceParser,
            String homeTag) throws IOException{
        this.mXmlParser = xmlResourceParser;
        this.mHomeTag = homeTag;
    }

    public synchronized boolean startParser(){
        try {
            int eventType = mXmlParser.getEventType();
            if (eventType != XmlPullParser.END_DOCUMENT) {
                if (!TextUtils.isEmpty(mHomeTag)) {
                    moveToNextStartTag(mHomeTag);
                }
                moveToNextStartTag(null);
            }
            return true;
        } catch ( XmlPullParserException e) {
            e.printStackTrace();
        }catch (IOException  e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean skipNearEndTag(String tagName)
            throws XmlPullParserException, IOException {
        int eventType = mXmlParser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            eventType = mXmlParser.next();
            if (eventType == XmlPullParser.END_TAG &&
                    (TextUtils.isEmpty(tagName)
                            || mXmlParser.getName().equals(tagName))) {
                return moveToNextStartTag(null);
            }
        }
        return false;
    }

    public boolean moveToNextStartTag(String tagName)
            throws XmlPullParserException, IOException {
        int eventType = mXmlParser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            eventType = mXmlParser.next();
            if (eventType == XmlPullParser.START_TAG &&
                    (TextUtils.isEmpty(tagName)
                            || mXmlParser.getName().equals(tagName))) {
                return true;
            }
        }
        return false;
    }

    public XmlTag parseTagAndMoveToNext()
            throws IOException, XmlPullParserException {
        XmlTag homeTag = null;
        int eventType = mXmlParser.getEventType();
        if (eventType != XmlPullParser.START_TAG && !moveToNextStartTag(null)) {
            return homeTag;
        }
        String name = mXmlParser.getName();
        int attributeCount = mXmlParser.getAttributeCount();
        Bundle bundle = new Bundle();
        for (int i = 0; i < attributeCount; i++) {
            String attrName = mXmlParser.getAttributeName(i);
            int attrRes = mXmlParser.getAttributeResourceValue(i, -1);
            if (attrRes == -1) {
                bundle.putString(attrName, mXmlParser.getAttributeValue(i));
            }else {
                bundle.putInt(attrName, attrRes);
            }
        }
        eventType = mXmlParser.next();
        if (eventType == XmlPullParser.END_TAG) {
            homeTag = NBXmlTag.obtain(name, null);
        } else {
            String text = null;
            if (eventType == XmlPullParser.TEXT) {
                text = mXmlParser.getText();
                eventType = mXmlParser.next();
            }
            if (eventType == XmlPullParser.START_TAG) {
                homeTag = NBXmlTag.obtain(name);
                while (eventType != XmlPullParser.END_TAG
                        && !name.equals(mXmlParser.getName())) {
                    homeTag.addSubTag(parseTagAndMoveToNext());
                }
            } else {
                homeTag = NBXmlTag.obtain(name, text);
            }
        }
        homeTag.addAttributes(bundle);
        nextTag();
        return homeTag;
    }

    private void nextTag() throws XmlPullParserException, IOException {
        int type = mXmlParser.next();
        if (type == XmlPullParser.TEXT) {
            mXmlParser.next();
        }
    }
}